'Copyright (c) 2009, Merrimack Valley Library Consortium
'Copyright (c) 2012, Jay Miley
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are permitted
'provided that the following conditions are met:
'
'    * Redistributions of source code must retain the above copyright notice, this list of
'      conditions and the following disclaimer.
'    * Redistributions in binary form must reproduce the above copyright notice, this list of
'      conditions and the following disclaimer in the documentation and/or other materials
'      provided with the distribution.
'    * Neither the name of the Merrimack Valley Library Consortium nor the names of its
'      contributors may be used to endorse or promote products derived from this software without
'      specific prior written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
'IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
'AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
'CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
'CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
'SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
'THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
'OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
'POSSIBILITY OF SUCH DAMAGE.

Imports System.Xml

Public Class frmAgencies
    Friend agencyFile As String = String.Empty
    Private agencyDocument As New XmlDocument

    Public Function loadFile(ByRef filename As String) As Boolean
        Try
            agencyDocument.Load(filename)
            Dim myNodes As XmlNodeList
            Dim name As XmlNode
            myNodes = agencyDocument.SelectNodes("Agencies/Agency")
            lstAgencies.Items.Clear()
            For Each agency As XmlNode In myNodes
                name = agency.SelectSingleNode("Name")
                If name Is Nothing Then
                    Continue For
                End If
                lstAgencies.Items.Add(name.InnerText)
            Next
            If lstAgencies.Items.Count = 0 Then
                Return False
            End If
            lstAgencies.SelectedIndex = 0
            agencyFile = filename
            Me.Text = "Symphony Receipt Printer - Agency Settings - " & agencyFile
            Return True
        Catch ex As Exception
            Dim message As String = ex.Message
            Return False
        End Try
    End Function

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If agencyFile.Length > 0 Then
            agencyDocument.Save(agencyFile)
        Else
            btnSaveAs_Click(sender, e)
        End If
    End Sub

    Private Sub btnSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAs.Click
        If saveConfigDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Try
                agencyDocument.Save(saveConfigDialog.FileName)
                agencyFile = saveConfigDialog.FileName
                Me.Text = "Symphony Receipt Printer - Agency Settings - " & agencyFile
            Catch ex As Exception
                MsgBox("Could not save: " & ex.Message, MsgBoxStyle.Critical, "Save Failure")
            End Try
        End If
    End Sub

    Private Sub lstAgencies_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstAgencies.SelectedIndexChanged
        Dim agency As XmlNode, agencydata As XmlNode
        agency = agencyDocument.SelectSingleNode("Agencies/Agency[Name=""" & lstAgencies.SelectedItem & """]")
        If Not agency Is Nothing Then
            agencydata = agency.SelectSingleNode("Name")
            If Not agencydata Is Nothing Then
                lblAgencyName.Text = agencydata.InnerText
            Else
                lblAgencyName.Text = ""
            End If
            agencydata = agency.SelectSingleNode("Route")
            If Not agencydata Is Nothing Then
                lblAgencyRoute.Text = agencydata.InnerText
            Else
                lblAgencyRoute.Text = ""
            End If
            agencydata = agency.SelectSingleNode("Branch")
            If Not agencydata Is Nothing Then
                lblAgencyBranch.Text = agencydata.InnerText
            Else
                lblAgencyBranch.Text = ""
            End If
            agencydata = agency.SelectSingleNode("To")
            If Not agencydata Is Nothing Then
                lblAgencyTo.Text = agencydata.InnerText
            Else
                lblAgencyTo.Text = lblAgencyName.Text
            End If
            agencydata = agency.SelectSingleNode("SlipType")
            If Not agencydata Is Nothing Then
                Select Case agencydata.InnerText.Trim
                    Case "1"
                        rdoMiniSlip.Checked = True
                    Case "2"
                        rdoNoSlip.Checked = True
                    Case "3"
                        rdoFullSlipNoName.Checked = True
                    Case "4"
                        rdoFullSlipLargeName.Checked = True
                    Case Else
                        rdoFullSlip.Checked = True
                End Select
            Else
                rdoFullSlip.Checked = True
            End If
            rdoMiniSlip.Enabled = True
            rdoNoSlip.Enabled = True
            rdoFullSlip.Enabled = True
        Else
            lblAgencyName.Text = ""
            lblAgencyRoute.Text = ""
            lblAgencyBranch.Text = ""
            lblAgencyTo.Text = ""
            rdoMiniSlip.Checked = False
            rdoNoSlip.Checked = False
            rdoFullSlip.Checked = False
            rdoMiniSlip.Enabled = False
            rdoNoSlip.Enabled = False
            rdoFullSlip.Enabled = False
        End If
    End Sub

    Private Sub rdoSlipType_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoFullSlip.CheckedChanged, rdoMiniSlip.CheckedChanged, rdoNoSlip.CheckedChanged, rdoFullSlipNoName.CheckedChanged, rdoFullSlipLargeName.CheckedChanged
        If lstAgencies.SelectedItem.ToString.Length <= 0 Or sender.Checked = False Then
            Return
        End If
        Dim myNode As XmlNode
        myNode = agencyDocument.SelectSingleNode("Agencies/Agency[Name=""" & lstAgencies.SelectedItem & """]")
        If Not myNode Is Nothing Then
            If sender Is rdoFullSlip Then
                frmSettings.insertReplaceValue(agencyDocument, myNode, "SlipType", "0")
            ElseIf sender Is rdoMiniSlip Then
                frmSettings.insertReplaceValue(agencyDocument, myNode, "SlipType", "1")
            ElseIf sender Is rdoNoSlip Then
                frmSettings.insertReplaceValue(agencyDocument, myNode, "SlipType", "2")
            ElseIf sender Is rdoFullSlipNoName Then
                frmSettings.insertReplaceValue(agencyDocument, myNode, "SlipType", "3")
            ElseIf sender Is rdoFullSlipLargeName Then
                frmSettings.insertReplaceValue(agencyDocument, myNode, "SlipType", "4")
            End If
        End If
    End Sub

End Class